/*
 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.dromara.warm.flow.solon;

import com.beetl.sql.pref.PerformanceConfig;
import org.beetl.sql.core.ConnectionSource;
import org.beetl.sql.core.ConnectionSourceHelper;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLManagerBuilder;
import org.beetl.sql.mapper.DefaultMapperBuilder;
import org.beetl.sql.mapper.proxy.MapperProxyConfigBuilder;
import org.dromara.warm.flow.core.invoker.FrameInvoker;
import org.dromara.warm.flow.orm.mapper.*;
import org.dromara.warm.plugin.modes.solon.config.BeanConfig;
import org.noear.solon.Solon;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;

import javax.sql.DataSource;

/**
 * Warm-Flow工作流插件
 *
 * @author warm
 */
public class XPluginImpl implements Plugin {


    @Override
    public void start(AppContext context) {
        context.beanMake(BeanConfig.class);
        Solon.context().getBeanAsync(DataSource.class, bean->{
            ConnectionSource source = ConnectionSourceHelper.getSingle(bean);
            SQLManagerBuilder builder = new SQLManagerBuilder(source);
            SQLManager sqlManager = builder.build();
            PerformanceConfig performanceConfig = new PerformanceConfig();
            performanceConfig.config(sqlManager);
            SQLManager.javabeanStrict(false);

            FrameInvoker.setBeanFunction((clazz) -> {
                if (clazz.isAssignableFrom(SQLManager.class)) {
                    return sqlManager;
                } else if (clazz.isAssignableFrom(FlowDefinitionMapper.class)) {
                    return sqlManager.getMapper(FlowDefinitionMapper.class);
                } else if (clazz.isAssignableFrom(FlowFormMapper.class)) {
                    return sqlManager.getMapper(FlowFormMapper.class);
                } else if (clazz.isAssignableFrom(FlowHisTaskMapper.class)) {
                    return sqlManager.getMapper(FlowHisTaskMapper.class);
                } else if (clazz.isAssignableFrom(FlowInstanceMapper.class)) {
                    return sqlManager.getMapper(FlowInstanceMapper.class);
                } else if (clazz.isAssignableFrom(FlowNodeMapper.class)) {
                    return sqlManager.getMapper(FlowNodeMapper.class);
                } else if (clazz.isAssignableFrom(FlowSkipMapper.class)) {
                    return sqlManager.getMapper(FlowSkipMapper.class);
                } else if (clazz.isAssignableFrom(FlowTaskMapper.class)) {
                    return sqlManager.getMapper(FlowTaskMapper.class);
                } else if (clazz.isAssignableFrom(FlowUserMapper.class)) {
                    return sqlManager.getMapper(FlowUserMapper.class);
                }
                return Solon.context().getBean(clazz);
            });
        });
    }
}
